package com.xmwzw.catpu;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.xmwzw.catpu.bean.SoftBean;
import com.xmwzw.catpu.databinding.ActivitySplashBinding;
import com.xmwzw.catpu.db.SoftModel;
import com.xmwzw.catpu.db.SoftModel_Table;
import com.xmwzw.catpu.http.BaseRequest;
import com.xmwzw.catpu.http.bean.TypeBean;
import com.xmwzw.catpu.http.listener.ItemListener;
import com.xmwzw.catpu.http.listener.TypeListener;
import com.xmwzw.catpu.util.Constant;
import com.xmwzw.catpu.util.SPFaceUtils;

import java.lang.ref.WeakReference;
import java.util.List;

public class SplashActivity extends BaseActivity {
    private SimpleHandler handler;
    private int count = 3;
    private ActivitySplashBinding splashBinding;
    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        splashBinding = getBaseViewBinding();
        handler = new SimpleHandler(this);
        handler.sendEmptyMessageDelayed(1, 1000);
    }

    @Override
    protected void onResume() {
        super.onResume();
        BaseRequest.getInstance().getTypes(new TypeListener() {
            @Override
            public void getType(List<TypeBean> typeBeans) {
                Constant.typeBeans.clear();
                Constant.typeBeans.addAll(typeBeans);

                for (TypeBean typeBean : typeBeans) {
                    getItems(Integer.valueOf(typeBean.getId()));
                }


            }
        });
    }

    private void getItems(int type) {
        BaseRequest.getInstance().getItems(type, new ItemListener() {
            @Override
            public void getItems(List<SoftBean> list) {
                for (int i = 0; i < list.size(); i++) {
                    SoftBean softBean = list.get(i);
                    softBean.no = (i + 1) + "";
                    insertOrUpdate(softBean);

                }

            }
        });
    }


    private void insertOrUpdate(SoftBean softBean) {
        SoftModel softModel = SQLite.select().from(SoftModel.class)
                .where(SoftModel_Table.id.eq(softBean.id))
                .querySingle();

        if (softModel == null) {
            new SoftModel(softBean).insert();
        } else {
            softModel.id = softBean.id;
            softModel.type_id = softBean.type_id;
            softModel.type_name = softBean.type_name;
            softModel.name = softBean.name;
            softModel.id = softBean.id;
            softModel.down_url = softBean.down_url;
            softModel.version = softBean.version;
            softModel.icon_url = softBean.icon_url;
            softModel.update();
        }
    }

    private static class SimpleHandler extends Handler {
        private WeakReference<SplashActivity> weakReference;

        public SimpleHandler(SplashActivity splashActivity) {
            weakReference = new WeakReference<>(splashActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                weakReference.get().count--;
                weakReference.get().splashBinding.tvTimer.setText(weakReference.get().count+"");
                if (weakReference.get().count > 0) {
                    sendEmptyMessageDelayed(1, 1000);
                } else {
                    weakReference.get().goHome();
                }
            }
        }
    }

    private void goHome() {
        boolean isLogin = SPFaceUtils.get(getApplicationContext(), Constant.KEY_LOGIN, false);
        long time = SPFaceUtils.get(getApplicationContext(), Constant.KEY_LOGIN_TIME, -1L);
        long currentTime = System.currentTimeMillis();

        Class clazz = (isLogin && (currentTime - time < 1000 * 60 * 60)) ? MainActivity.class : LoginActivity.class;
        startActivity(new Intent(SplashActivity.this, clazz));
        //startActivity(new Intent(SplashActivity.this,MainActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }
}
